﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class JobController : Controller
    {

        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblJob.GetJobList(keywords, fid, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var job = id == 0
                ? new GswTblJob{ PubDate= DateTime.Today, CustomerId = GswTblUser.CurrentUser.CustomerId, EndDate = DateTime.Today} : GswTblJob.SingleOrDefault(id);
           

            return View(job);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;


            var job = id == 0 ? new GswTblJob() : GswTblJob.SingleOrDefault(id);
            job.JobTitle = c["JobTitle"];
            job.JobNum = InputValid.StrToInt(c["JobNum"], 0);
            job.PubDate = DateTime.Parse(c["PubDate"]);
            job.EndDate = DateTime.Parse(c["EndDate"]);
            job.JobDesc = c["JobDesc"];
            
            job.FunctionId = int.Parse(fid);
            job.CustomerId = GswTblUser.CurrentUser.CustomerId;
            


            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        job.Insert();
                        job = new GswTblJob { PubDate = DateTime.Today, CustomerId = GswTblUser.CurrentUser.CustomerId, EndDate = DateTime.Today };

                    }
                    else
                    {
                        job.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";

            return View(job);

        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblJob.Delete(id);
            var qry = GswTblJob.GetJobList(keywords, fid, GswTblUser.CurrentUser.CustomerId);
            return View("Index", qry);
        }

    }
}
